package com.dxy.controller;

import com.dxy.bean.*;

import com.dxy.mvc.ResponseBody;
import com.dxy.service.UserService;
import com.dxy.service.impl.UserServiceImpl;
import com.dxy.utils.DateFormatUtil;
import com.dxy.utils.JSONUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author DELL
 */
public class UserController {
    UserService userService = new UserServiceImpl();
    @ResponseBody("/user/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        List<Map<String, Integer>> data = userService.console();
        Message msg = new Message();
        if(data.size() == 0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/user/list.do")
    public String list (HttpServletRequest request ,HttpServletResponse response){
        //1. 获取查询数据的起始索引值
        int offset  = Integer.parseInt(request.getParameter("offset"));
        //2.    获取当前页要查询的数据量
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //3.    进行查询
        List<User> list = userService.findByUserAll(true,offset,pageNumber);
        List<BootStrapUser> list2 = new ArrayList<>();
        for (User user : list) {
            String registeredTime = DateFormatUtil.format(user.getRegisteredTime());
            String loginTime = user.getLoginTime() == null ? "未登录" : DateFormatUtil.format(user.getLoginTime());
            String status = user.getStatus() == 0 ? "在线" : "离线";
            BootStrapUser bootStrapUser = new BootStrapUser(user.getId(),user.getUsername(), user.getUserPhone(), user.getPassword(), user.getIcCard(), registeredTime, loginTime, status);
            list2.add(bootStrapUser);
        }
        List<Map<String,Integer>> console =userService.console();
        Integer total = console.get(0).get("data1_size");
        //  4.将集合封装为bootStrap - table识别的格式
        ResultData<BootStrapUser> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    /**
     * 根据手机号查询用户
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/user/find.do")
    public String find(HttpServletRequest request,HttpServletResponse response){
        //接收参数
        String userPhone = request.getParameter("userPhone");
        List<User> user =  userService.findByUserPhone(userPhone);
        Message msg = new Message();
        if(user.size()==0){
            msg.setStatus(-1);
            msg.setResult("用户不存在");
        }else{
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(user);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    /**
     * 修改用户的信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest request,HttpServletResponse response){
        //1. 接收参数
        int id =Integer.parseInt(request.getParameter("id"));
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String userPhone = request.getParameter("userPhone");
        String icCard = request.getParameter("icCard");
        // 修改后的用户对象
        User newUser = new User();
        newUser.setUsername(username);
        newUser.setUserPhone(userPhone);
        newUser.setIcCard(icCard);
        newUser.setPassword(password);
        //2.  调用userService修改信息
        boolean flag = userService.update(id, newUser);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("修改失败");

        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }
    @ResponseBody("/user/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String icCard = request.getParameter("icCard");
        String password = request.getParameter("password");
        User user = new User(username,userPhone,icCard,password);
        boolean flag = userService.insert(user);
        Message msg = new Message();
        if(flag){
            //录入成功
            msg.setStatus(0);
            msg.setResult("添加成功!");
        }else{
            //录入失败
            msg.setStatus(-1);
            msg.setResult("添加失败!");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest request,HttpServletResponse response){
        // 获取请求参数
        String id = request.getParameter("id");
        int i = Integer.parseInt(id);
        // 调用userService.delete方法根据id删除
        boolean flag = userService.delete(i);
        // 创建消息
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }


}
